/*
 *  * Copyright (C) 2021 Huawei Device Co., Ltd.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package com.jaredrummler.materialspinner.example.slice;

import com.github.clans.fab.FloatingActionButton;
import com.github.clans.fab.FloatingActionMenu;
import com.jaredrummler.materialspinner.MaterialSpinner;
import com.jaredrummler.materialspinner.ResTUtil;
import com.jaredrummler.materialspinner.example.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.utils.net.Uri;

import java.util.Timer;
import java.util.TimerTask;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT;

public class MainAbilitySlice extends AbilitySlice {
    private ToastDialog toastDialog;
    MaterialSpinner spinner;
    FloatingActionButton floatingActionButton;
    private static final String[] VERSIONS = {
            "Cupcake",
            "Donut",
            "Eclair",
            "Froyo",
            "Gingerbread",
            "Honeycomb",
            "Ice Cream Sandwich",
            "Jelly Bean",
            "KitKat",
            "Lollipop",
            "Marshmallow",
            "Nougat",
            "Oreo"
    };

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        floatingActionButton = (FloatingActionButton) findComponentById(ResourceTable.Id_fab22);
        floatingActionButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
            }
        });
        spinner = (MaterialSpinner) findComponentById(ResourceTable.Id_spinner);
        spinner.setBackgroundSelector(ResourceTable.Graphic_selectdialog_changecolor);
        spinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(MaterialSpinner view, int position, String text) {
                showToast("Clicked: " + text);
            }

        });
        spinner.setOnNothingSelectedListener(new MaterialSpinner.OnNothingSelectedListener() {
            @Override
            public void onNothingSelected() {
                showToast("Nothing Selected");
            }
        });
        spinner.setItems(VERSIONS);
    }


    private void showToast(String message) {
        floatingActionButton.setMarginBottom(100);
        if (toastDialog != null) {
            toastDialog.cancel();
            toastDialog = null;
        }
        toastDialog = new ToastDialog(this);
        ComponentContainer componentContainer = (ComponentContainer) LayoutScatter.getInstance(this).parse(com.jaredrummler.materialspinner.ResourceTable.Layout_item_toast, null, false);
        Text messageTV = (Text) componentContainer.findComponentById(com.jaredrummler.materialspinner.ResourceTable.Id_toast_text);
        DependentLayout background = (DependentLayout) componentContainer.findComponentById(com.jaredrummler.materialspinner.ResourceTable.Id_background);
        messageTV.setText(message);
        ShapeElement infoShapeElement = new ShapeElement();
        infoShapeElement.setRgbColor(new RgbColor(0, 0, 0));
        background.setBackground(infoShapeElement);

        toastDialog.setContentCustomComponent(componentContainer);
        toastDialog.setAlignment(TextAlignment.BOTTOM);
        toastDialog.setSize(1000, 140).setDuration(500).show();
        TaskDispatcher dispatcher = this.getUITaskDispatcher();
        dispatcher.delayDispatch(new Runnable() {
            @Override
            public void run() {
                floatingActionButton.setMarginBottom(0);
            }
        }, 2000);


    }
}
